package com.ly.houses.service.impl;

import com.ly.houses.common.OssService;
import com.ly.houses.mapper.BuildingMapper;
import com.ly.houses.mapper.HouseMapper;
import com.ly.houses.mapper.PlateMapper;
import com.ly.houses.model.Building;
import com.ly.houses.model.House;
import com.ly.houses.model.Plate;
import com.ly.houses.service.PlateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PlateServiceImpl implements PlateService {
    @Resource
    private PlateMapper plateMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private HouseMapper houseMapper;


    @Override
    public void batchSave(List<Plate> plates) {
        plateMapper.batchSave(plates);
    }

    @Override
    public void batchUpdate(List<Plate> plates) {
        plateMapper.batchUpdate(plates);
    }

    @Override
    public List<Plate> selectAllPlace(Long areaId) {
        return plateMapper.selectAllPlace(areaId);
    }


    @Override
    public List<Plate> listPlateByName(String name,Long areaId) {
        List<Plate> plates=  plateMapper.listPlateByName(name,areaId);

        return plates;
    }

    @Override
    public String getAvgPrice(Long plateId) {
       Integer price= plateMapper.getAvgPrice(plateId);
       if(price!=null&&price!=0){
           return price.toString();
       }
       else{
           return null;
       }
    }

    @Override
    public void updateAvgPrice(List<Plate> plates) {
        plateMapper.updateAvgPrice(plates);
    }

    @Override
    public List<String> selectAllPlaceName(Long areaId) {

        return plateMapper.selectAllPlaceName(areaId);
    }

    @Override
    public void removePlacesByName(List<String> names,Long areaId) {
        List<Plate> removePlateList=new ArrayList<>();
        List<Building> removeBuildingList=new ArrayList<>();
        List<House> removeHouseList=new ArrayList<>();

        for(String name:names){
            Plate plate=plateMapper.getByName(name,areaId);
            if(plate!=null){
                removePlateList.add(plate);
                List<Building> buildings=buildingMapper.listBuildingByPlateId(plate.getId());
                if(buildings!=null&&buildings.size()>0){
                    for(Building building:buildings){
                        List<House> houses=houseMapper.listHouseByBuildingId(building.getId());
                        if(houses!=null&&houses.size()>0){
                            removeHouseList.addAll(houses);
                        }
                    }
                    removeBuildingList.addAll(buildings);
                }
            }
        }
        if(removePlateList.size()>0){
            plateMapper.removeAll(removePlateList);
        }
        if(removeBuildingList.size()>0){
            buildingMapper.removeAll(removeBuildingList);
        }
        if(removeHouseList.size()>0){
            houseMapper.removeAll(removeHouseList);
        }
    }
}
